package com.sh.wy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.wy.system.entity.SysRoleParam;
import com.sh.wy.system.entity.SaveRoleIdsByUserIdParam;
import com.sh.wy.system.entity.SysRole;
import com.sh.wy.system.entity.SysUserRole;
import com.sh.wy.system.mapper.SysRoleMapper;
import com.sh.wy.system.mapper.SysUserRoleMapper;
import com.sh.wy.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.VarHandle;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 世豪学Java
 * @since 2023-12-19
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public IPage<SysRole> list(SysRoleParam sysRoleParam) {
        //分页工具
        IPage<SysRole> page = new Page<>(sysRoleParam.getCurrentPage(), sysRoleParam.getPageSize());
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if (Strings.hasText(sysRoleParam.getRoleName())) {
            wrapper.lambda().like(SysRole::getRoleName, sysRoleParam.getRoleName());
        }
        return this.baseMapper.selectPage(page, wrapper);
    }

    //查询所有角色并选中用户拥有的角色
    @Override
    public List<SysRole> checkRoles(Integer userId) {
        List<SysRole> roles = this.baseMapper.selectList(null);
        //查询用户拥有的角色信息id
        List<Integer> sysUserRoles = userRoleMapper.selectRoleIds(userId);
        //便利roles，如果拥有角色信息的id，就设置checked为true
        List<SysRole> result = roles.stream().map(role -> {
            if (sysUserRoles.contains(role.getRoleId())) {
                role.setChecked(true);
            }
            return role;
            //变成集合
        }).collect(Collectors.toList());
        return result;
    }

    //添加用户对应的角色
    @Override
    public boolean saveRoleIdsByUserId(SaveRoleIdsByUserIdParam param) {
        //先删除用户角色中间表中的对应的信息
        LambdaQueryWrapper<SysUserRole> query = new LambdaQueryWrapper<>();
        query.eq(SysUserRole::getUserId,param.getUserId());
        int delete = userRoleMapper.delete(query);
        System.out.println(delete);

        //往数据库添加用户对应的角色
        //全部在param.getRoleIds()中
        List<Integer> roleIds = param.getRoleIds();
        if (!Objects.isNull(roleIds) && roleIds.size()>0){
            for(Integer roleId : roleIds){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(param.getUserId());
                sysUserRole.setRoleId(roleId);
                userRoleMapper.insert(sysUserRole);
            }
        }

        return true;
    }


}
